package com.leyou.controller;

import com.leyou.dto.AddressDTO;
import com.leyou.dto.SaveAddressDto;
import com.leyou.entity.Address;
import com.leyou.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("myAddress")
    public ResponseEntity<String> hello(){
        return ResponseEntity.ok("上海浦东新区航头镇航头路安居公寓");
    }

    /**
     * 获取全部的地址，展示到前台
     */
    @GetMapping("/allAddress")
    public ResponseEntity<List<AddressDTO>> queryAllAddress(){
        List<AddressDTO> list=  addressService.queryAllAddress();
        return ResponseEntity.ok(list);
    }
    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAddressById(@PathVariable("id") Long id){
        addressService.removeById(id);
        return ResponseEntity.ok().build();
    }
    /**
     * 新增地址
     */
    @PostMapping("/save")
    public ResponseEntity<Void> saveAddress(@RequestBody SaveAddressDto saveAddressDto){
        System.out.println("saveAddressDto = " + saveAddressDto);
        addressService.saveAddress(saveAddressDto);
        return ResponseEntity.ok().build();
    }
    /**
     * 根据收货人id查询，返回收获的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
   public ResponseEntity<AddressDTO>  getMyAddressById(@PathVariable("id") Long id){
        Address address1 = addressService.query().eq("id", id).one();
        AddressDTO addressDTO = new AddressDTO(address1);
//        AddressDTO address = new AddressDTO();
//        address.setId(1L);
//        address.setUserId(30L);
//        address.setStreet("航头镇航头路18号传智播客 3号楼");
//        address.setCity("上海");
//        address.setDistrict("浦东新区");
//        address.setAddressee("虎哥");
//        address.setPhone("15800000000");
//        address.setProvince("上海");
//        address.setPostcode("210000");
//        address.setIsDefault(true);
        return ResponseEntity.ok(addressDTO);
    }
}